package cn.ffcs.contract.service;


import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import cn.ffcs.base.service.BaseService;
import cn.ffcs.base.web.PageControl;
import cn.ffcs.bpm.model.ProcTranRecord;
import cn.ffcs.bpm.model.ProcessConfig;
import cn.ffcs.bpm.model.ProcessConfigDetail;
import cn.ffcs.bpm.model.TransactProcessStatus;
import cn.ffcs.businessrequest.model.BusinessRequest;
import cn.ffcs.businessrequest.model.ReqProgress;
import cn.ffcs.common.dao.AttachDAO;
import cn.ffcs.common.model.Attach;
import cn.ffcs.contract.dao.ContractInfoDAO;
import cn.ffcs.contract.dao.ContractRecordDAO;
import cn.ffcs.contract.model.ContractInfo;
import cn.ffcs.contract.model.ContractUpload;
import cn.ffcs.organ.dao.PersonDAO;
import cn.ffcs.organ.model.Organization;
import cn.ffcs.organ.model.Person;
import cn.ffcs.security.dao.UserDAO;
import cn.ffcs.security.model.User;
import cn.ffcs.system.dict.dao.DictDAO;
import cn.ffcs.system.dict.model.Dict;
import cn.ffcs.util.converter.DateUtil;

/**
 * 服务层<br/>
 * <table>
 * <tr>
 * <td>负责人/principal:</td>
 * <td colspan="2">pany</td>
 * <tr>
 * <td>修改记录/revision:</td>
 * <td colspan="2"></td>
 * </tr>
 * <tr>
 * <td>日期:</td>
 * <td>修改人:</td>
 * <td>修改说明:</td>
 * </tr>
 * <tr>
 * <td></td>
 * <td></td>
 * <td></td>
 * </tr>
 * </table>
 * @author pany
 * @version $Id$
 */
public class ContractInfoService<D extends ContractInfo> extends BaseService<D>
implements IContractInfoService<D>
{
    @SuppressWarnings("unchecked")
	private ContractInfoDAO contractInfoDAO;
    @SuppressWarnings("unchecked")
	private ContractRecordDAO contractRecordDAO;
    @SuppressWarnings("unchecked")
	private AttachDAO attachDAO;
    
    private PersonDAO<Person> personDAO;
    
    private DictDAO<Dict> dictDao;
    
    private UserDAO<User> userDao;
    public UserDAO<User> getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDAO<User> userDao) {
		this.userDao = userDao;
	}
	/**
     * 附件删除
     * @param delIDs
     * @param saveDirectory
     */
    public void deleteContractUploads(String[] delIDs,String saveDirectory){
    	for (int i = 0; i < delIDs.length; i++) {
			String id=delIDs[i];
			ContractUpload contractUpload=getContractInfoDAO().getContractUpload(id);
			File f=new File(saveDirectory+contractUpload.getMonthFile()+"/"+contractUpload.getRealName());
			f.delete();
			getContractInfoDAO().deleteContractUpload(id);
		}
    }
    /**
     * 删除操作
     * @param baseEntity
     */
    public void remove(D contractInfo){
        if (contractInfo!=null){
            attachDAO.deleteAttachByEntity(contractInfo);
            contractRecordDAO.remove(null, "contract", contractInfo.getId()+"");
            
            super.remove(contractInfo);
        }
    }
    @SuppressWarnings("unchecked")
	public void impDataFromExcel(String path){
    	//D盘模版的导入
//       	try {
//			Workbook book = Workbook.getWorkbook(new File(path));
//			Sheet sheet = book.getSheet(0);
//			System.out.println(sheet.getRows());;
//			System.out.println(sheet.getColumns());;
//			ContractInfo contractInfo=null;
//			for (int j = 1; j < sheet.getRows(); j++) {
//				for (int i = 12; i < 14; i++) {
//					System.out.println("["+i+","+j+"]"+sheet.getCell(i,j).getContents());
//				}
//				contractInfo=new ContractInfo();
//				contractInfo.setContractName(sheet.getCell(0, j).getContents());
//				contractInfo.setSerial(sheet.getCell(1,j).getContents());;
//				contractInfo.setSignCode(sheet.getCell(2,j).getContents());
//				
//				contractInfo.setSignatoriesName(sheet.getCell(6, j).getContents());
//				contractInfo.setThirdName(sheet.getCell(7, j).getContents());
//				contractInfo.setHezuoContent(sheet.getCell(8, j).getContents());
//				contractInfo.setHezuoContentInfo(sheet.getCell(9, j).getContents());
//				contractInfo.setHezuoMode(sheet.getCell(10, j).getContents());
//				contractInfo.setHezuoModeInfo(sheet.getCell(11, j).getContents());
//				String effectTime=sheet.getCell(12, j).getContents();
//				if(effectTime!=null&&!"".equals(effectTime)){
//					contractInfo.setEffectTime(DateUtil.strToDateTime(effectTime+" 08:00:00", "dd/MM/yyyy HH:mm:ss"));
//				}
//				String maturityTime=sheet.getCell(13, j).getContents();
//				if(maturityTime!=null&&!"".equals(maturityTime)){
//					contractInfo.setMaturityTime(DateUtil.strToDateTime(maturityTime+" 08:00:00", "dd/MM/yyyy HH:mm:ss"));
//				}
//				contractInfo.setContact(sheet.getCell(14, j).getContents());
//				contractInfo.setContactTel(sheet.getCell(15, j).getContents());
//				String postponeTime=sheet.getCell(16, j).getContents();
//				if(postponeTime!=null&&!"".equals(postponeTime)){
//					contractInfo.setPostpone("是");
//					contractInfo.setPostponeTime(DateUtil.strToDateTime(postponeTime+" 08:00:00", "dd/MM/yyyy HH:mm:ss"));
//				}else{
//					contractInfo.setPostpone("否");
//					if(maturityTime!=null&&!"".equals(maturityTime)){
//						contractInfo.setPostponeTime(DateUtil.strToDateTime(maturityTime+" 08:00:00", "dd/MM/yyyy HH:mm:ss"));
//					}
//				}
//				contractInfo.setNotes(sheet.getCell(17, j).getContents());
//				contractInfo.setCondInfo(sheet.getCell(18, j).getContents());
//				
//				contractInfo.setSubmitTime(new Date());
//				contractInfo.setStatus("2");
//				contractInfo.setCreateTime(new Date());
//				contractInfo.setBegin(new Date());
//				contractInfo.setEnd(DateUtil.getMaxDate());
//				contractInfo.setNextUser("[ADMIN]");
//				contractInfo.setCreator("ADMIN");
//				contractInfo.setCreatorName("管理员");
//				getContractInfoDAO().save(contractInfo);
//			}
//			book.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
/////////////////////////////////数据迁移
    	try {
			Workbook book = Workbook.getWorkbook(new File(path));
			Sheet sheet = book.getSheet(0);
			System.out.println(sheet.getRows());;
			System.out.println(sheet.getColumns());;
			ContractInfo contractInfo=null;
			String str_date="";
			Date date=null;
			Person person=null;
			Dict dict=null;
			System.out.println("[14,8]"+sheet.getCell(14,8).getContents());
			for (int j = 3; j < sheet.getRows(); j++) {//行
//				for (int i = 14; i < 16; i++) {
//					if(j<20){
//					System.out.println("["+i+","+j+"]"+sheet.getCell(i,j).getContents());
//					}
//				}
				
				contractInfo=new ContractInfo();
				contractInfo.setContractName(sheet.getCell(1, j).getContents());
				str_date=sheet.getCell(24, j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					dict=getDictDao().getDictByName(str_date, "11");
					contractInfo.setContractType(dict);
				}
				contractInfo.setSerial(sheet.getCell(25, j).getContents());
				contractInfo.setSignCode(sheet.getCell(26, j).getContents());
				str_date=sheet.getCell(27, j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					
//					String creator=getPersonDAO().getUserId(str_date);
					person=getPersonDAO().getPersonByName(str_date);
					if(person!=null){
						User user=getUserDao().getUser(String.valueOf(person.getId()));
						contractInfo.setCreator(user.getUserID());
					}
				}
				contractInfo.setCreatorName(sheet.getCell(27, j).getContents());
				contractInfo.setVersion(sheet.getCell(28, j).getContents());
				contractInfo.setAmount(sheet.getCell(29, j).getContents());
				str_date=sheet.getCell(30,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setSubmitTime(date);
				}else{
					contractInfo.setSubmitTime(new Date());
				}
				str_date=sheet.getCell(32,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setArrivalTime(date);
				}
				str_date=sheet.getCell(33,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setProcessTime(date);
				}
				str_date=sheet.getCell(35,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setEffectTime(date);
				}
				str_date=sheet.getCell(36,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setMaturityTime(date);
				}
				str_date=sheet.getCell(37,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					if(str_date.equals("1")){
						contractInfo.setPostpone("是");
					}else{
						contractInfo.setPostpone("否");
					}
				}else{
					contractInfo.setPostpone("否");
				}
				str_date=sheet.getCell(38,j).getContents();
				if(str_date!=null&&!"".equals(str_date)){
					date=DateUtil.strToDate(str_date, "MM/dd/yyyy");
					contractInfo.setPostponeTime(date);
				}
				contractInfo.setSignatoriesName(sheet.getCell(39, j).getContents());
				
				
				
				
				//contractInfo.setSubmitTime(new Date());
				contractInfo.setStatus("2");
				contractInfo.setCreateTime(new Date());
				contractInfo.setBegin(new Date());
				contractInfo.setEnd(DateUtil.getMaxDate());
				contractInfo.setNextUser("[ADMIN]");
				
				getContractInfoDAO().save(contractInfo);
				str_date=sheet.getCell(17, j).getContents();
				String[] realNames=str_date.split(",");
				str_date=sheet.getCell(18, j).getContents();
				String[] fileNames=str_date.split(",");
				if(realNames.length!=fileNames.length){
					System.out.println("第"+j+"行文件数据不相同");
				}
				if(!str_date.equals("")){
					for (int i = 0; i < realNames.length; i++) {
						ContractUpload contractUpload=new ContractUpload();
						contractUpload.setContractInfoId(contractInfo.getId().toString());
						System.out.println("行："+j);
						contractUpload.setRealName(realNames[i]);
						//contractUpload.setFileName(fileNames[i]);
						getAttachDAO().getHibernateTemplate().save(contractUpload);
					}
				}
			}
			book.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public PageControl findByPageView(Criterion cond,int currPage, int pageSize,String orderBy){
    	return getContractInfoDAO().findByPageView(cond, currPage, pageSize,orderBy);
    }
    public AttachDAO getAttachDAO()
    {
        return attachDAO;
    }
    public void setAttachDAO(AttachDAO attachDAO)
    {
        this.attachDAO = attachDAO;
    }
    public ContractInfoDAO getContractInfoDAO()
    {
        return contractInfoDAO;
    }
    public void setContractInfoDAO(ContractInfoDAO contractInfoDAO)
    {
        this.contractInfoDAO = contractInfoDAO;
    }
    public ContractRecordDAO getContractRecordDAO()
    {
        return contractRecordDAO;
    }
    public void setContractRecordDAO(ContractRecordDAO contractRecordDAO)
    {
        this.contractRecordDAO = contractRecordDAO;
    }
	public PageControl findByPageView(Criterion cond, int currPage, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}
	public PersonDAO<Person> getPersonDAO() {
		return personDAO;
	}
	public void setPersonDAO(PersonDAO<Person> personDAO) {
		this.personDAO = personDAO;
	}
	public DictDAO<Dict> getDictDao() {
		return dictDao;
	}
	public void setDictDao(DictDAO<Dict> dictDao) {
		this.dictDao = dictDao;
	}
}
